﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête photos sur un album</summary>
    /// <see cref="upload"></see>
    [DataContract]
    public class album_photos : photo
    {        
        ///<summary>date d'upload de la photo</summary>
        [DataMember]
        public string date { get; set; }

        ///<summary>date EXIF de la photo si disponible</summary>
        [DataMember]
        public string exif_date { get; set; }

        ///<summary>login de l'utilisateur qui a uploadé la photo</summary>
        [DataMember]
        public string user { get; set; }
    }

    public delegate void AlbumPhotosEventHandler(object sender, AlbumPhotosEventArgs e);
    public class AlbumPhotosEventArgs : EventArgs
    {
        public AlbumPhotosEventArgs(ObservableCollection<album_photos> ap)
        {
            _result = ap;
        }

        private ObservableCollection<album_photos> _result;
        public ObservableCollection<album_photos> result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event AlbumPhotosEventHandler AlbumPhotoCompleted;

        #region album_photo
        /// <summary>Liste des photos d'un album.</summary>
        /// <c>http://apitxt.hellotipi.com/photo/album_photos.json?site=demo&album_id=12345</c>
        /// <param name="site">Site de famille dont la liste des albums est à recupérer</param>
        /// <param name="album">id de l'album</param>
        /// <returns>La liste des photos de l'album <paramref name="album"/> du site de famille <paramref name="site"/></returns>
        public void AlbumPhoto(string site, int album_id)
        {

            base.APICallCompleted += new APICallCompletedEventHandler(AlbumPhotoCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("site", site),
                                 new KeyValuePair<string, string>("album_id", album_id.ToString())
                             };

            GetUrlApi("photo/album_photos.json", querystring);
        }

        public void AlbumPhoto(string site)
        {

            base.APICallCompleted += new APICallCompletedEventHandler(AlbumPhotoCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("site", site),
                             };

            GetUrlApi("photo/album_photos.json", querystring);
        }

        void AlbumPhotoCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                AlbumPhotoCompleted.Invoke(sender, new AlbumPhotosEventArgs(serializeTools.Deserialize<ObservableCollection<album_photos>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(AlbumPhotoCallCompleted);
            }
        }

        #endregion

    }
}
